package work.lpssfxy.www.campuslifeassistantserver.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import work.lpssfxy.www.campuslifeassistantserver.base.qq.QQHttpClient;
import work.lpssfxy.www.campuslifeassistantserver.base.qq.QQStateErrorException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * created by on 2021/8/13
 * 描述：QQ
 *
 * @author ZSAndroid
 * @create 2021-08-13-17:01
 */
@Controller
public class QQController {
    //QQ互联中提供的 appid 和 appkey
    @Value("${qq.oauth.appid}")
    public String APPID;
    @Value("${qq.oauth.appkey}")
    public String APPKEY;
    //QQ互联中的回调地址
    @Value("${qq.oauth.url}")
    public String URL;

    /**
     * 发起请求
     * @param session
     * @return
     */
    @GetMapping(value = "/qq/login")
    public String qq(HttpSession session) throws UnsupportedEncodingException {

        //用于第三方应用防止CSRF攻击
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        session.setAttribute("state",uuid);

        //Step1：获取Authorization Code
        String url = "https://graph.qq.com/oauth2.0/authorize?response_type=code"+
                "&client_id=" + APPID +
                "&redirect_uri=" + URLEncoder.encode(URL,"UTF-8") +
                "&state=" + uuid;

        return "redirect:" + url;
    }

    /**
     * QQ回调
     * @param request
     * @return
     */
    @GetMapping(value = "/qq/callback")
    public String qqcallback(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        //qq返回的信息
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        String uuid = (String) session.getAttribute("state");

        if(uuid != null){
            if(!uuid.equals(state)){
                throw new QQStateErrorException("QQ,state错误");
            }
        }

        //Step2：通过Authorization Code获取Access Token
        String url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code"+
                "&client_id=" + APPID +
                "&client_secret=" + APPKEY +
                "&code=" + code +
                "&redirect_uri=" + URL;

        String access_token = QQHttpClient.getAccessToken(url);

        //Step3: 获取回调后的 openid 值
        url = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
        String openid = QQHttpClient.getOpenID(url);

        //Step4：获取QQ用户信息
        url = "https://graph.qq.com/user/get_user_info?access_token=" + access_token +
                "&oauth_consumer_key="+ APPID +
                "&openid=" + openid;

        /**
         * 返回用户的信息
         */
        JSONObject jsonObject = QQHttpClient.getUserInfo(url);
        //也可以放到Redis和mysql中，只取出了部分数据，根据自己需要取
        session.setAttribute("openid",openid);  //openid,用来唯一标识qq用户
        session.setAttribute("nickname",(String)jsonObject.get("nickname")); //QQ名
        session.setAttribute("figureurl_qq_2",(String)jsonObject.get("figureurl_qq_2")); //大小为100*100像素的QQ头像URL
        //重定向转home.html
        return "redirect:/home";
    }
}
